#include "protocol645.h"
#include <QTime>
protocol645::protocol645()
{

    setMainLayout  = new QVBoxLayout();
    test_show = new QTextBrowser();

    setMainLayout->addWidget(test_show);
    setLayout(setMainLayout);   //展示界面。
    resize(1000,500);
    setWindowTitle("645协议抄读数据显示界面");

//     timerid645 = startTimer(5000);
   this->setAttribute(Qt::WA_QuitOnClose, false);

}
//50ms定时器到了。
sendItemRes protocol645::prepareSend(INT8U readType,INT8U addr_no[][6],INT8U addr_cnt,INT8U next_frame_no)
{
    static INT8U addrIdx=0;
    static INT8U itemIdx=0,itemIdx_TX=0;
    //地址数组---后面传进来
//    INT8U addr_no[][6]={{0x00,0x00,0x09,0x04,0x08,0x00},
//                        {0x23,0x08,0x12,0x01,0x65,0x85},
//                       };

     sendItemRes tmpItem645;
     qDebug()<<"start"+QTime:: currentTime (). toString ( "HH:mm:ss" );
    //首先上来通配读通信单元的地址。
     if(readType == READADDR)
     {
        make645FrameAndSend((INT8U *)"\xAA\xAA\xAA\xAA\xAA\xAA",0x13, NULL,0);
         tmpItem645.item.itemName="抄通信单元地址";
        return tmpItem645;
     }
     else if(readType == READDOC)
     {
         make645FrameAndSend((INT8U *)"\xAA\xAA\xAA\xAA\xAA\xAA",0x11,(INT8U *)"\x04\xA0\x03\x01",4);
         tmpItem645.item.itemName="抄表档案";
         return tmpItem645;
     }
     else if(readType == READDOCNEXT)
     {
         INT8U item_nextframe[5]={0};
         memcpy(item_nextframe,(INT8U *)"\x04\xA0\x03\x01\x01",5);
         item_nextframe[4] = next_frame_no;
         make645FrameAndSend((INT8U *)"\xAA\xAA\xAA\xAA\xAA\xAA",0x12,item_nextframe,5);
         tmpItem645.item.itemName="抄表档案--后续帧";
         return tmpItem645;
     }
     else if (readType == READDATE_TX)   //读取通信单元的协议一致性
     {
         make645FrameAndSend((INT8U *)&addr_no[0][0],0x11,(INT8U *)&item645_TX[itemIdx_TX].item,4);
         tmpItem645.item = item645_TX[itemIdx_TX];
         memcpy(tmpItem645.addr , addr_no[0],6);

         if((++itemIdx_TX) ==ITEMAX_TX)
         {
             itemIdx_TX=0;
         }

     }
     else{
    //然后读取通信单元的表档案。
       qDebug()<< "addrIdx  is  "<<addrIdx <<"itemIdx  is  "<<itemIdx <<" (sizeof(addr_no)/6) "<<(sizeof(addr_no)/6) <<" (sizeof(item645)/4) "<<(sizeof(item645_JC)/4);
       make645FrameAndSend((INT8U *)&addr_no[addrIdx][0],0x11,(INT8U *)&item645_JC[itemIdx].item,4);
       tmpItem645.item = item645_JC[itemIdx];
       memcpy(tmpItem645.addr , addr_no[addrIdx],6);

       if((++itemIdx) ==ITEMAX)
       {
           addrIdx++;
           itemIdx=0;
           if(addrIdx == addr_cnt)
           {
                addrIdx=0;
           }
       }
     }
    return tmpItem645;
}
void protocol645::make645FrameAndSend(INT8U *meterNo,INT8U ctl,INT8U *Item,INT8U ItemLen)
{
    objRequest request;
     memset(serial_channel.response_frame,0x00,sizeof(serial_channel.response_frame));
        // 组报文
   int pos =0;
    // 组报文
   request.send_frame[pos++] = 0x68;
   request.send_frame[pos++] = meterNo[5];
   request.send_frame[pos++] = meterNo[4];
   request.send_frame[pos++] = meterNo[3];
   request.send_frame[pos++] =meterNo[2];
   request.send_frame[pos++] = meterNo[1];
   request.send_frame[pos++] = meterNo[0];
   request.send_frame[pos++] = 0x68;
   request.send_frame[pos++] = ctl;   //SEQ
   request.send_frame[pos++] = ItemLen;
   for(INT8U i=0;i<ItemLen;i++)
   {
     request.send_frame[pos++] = Item[3-i] +0x33;
   }
    request.send_frame[pos++] =  crc16_param(request.send_frame,pos);
    request.send_frame[pos++]=0x16;
      //发送到串口。
    memcpy(serial_channel.response_frame,request.send_frame,pos);
    serial_channel.response_len = pos;

    qDebug()<<"over "<<" pos is "<<pos;
}
INT8U protocol645::isProtocol645(INT8U *frame,INT16U frameLen)
{
    QString addr;
    for(int i=0;i<frameLen;i++)
    {
        addr.append(QString("%1").arg(static_cast<uchar>( frame[i]), 2, 16, QLatin1Char('0')));
        addr.append(' ');
    }
    if((frame[frameLen-1] == 0x16) &&(crc16_param(frame,frameLen-2) == frame[frameLen-2]))
    {
        return TRUE;
    }
    return FALSE;
}
void protocol645::logSlot(QString log)
{
    test_show->append(log);
    if(test_show->document()->lineCount()>5000)
    {
        QFile file("E:/LOG.txt");
        file.open(QIODevice::ReadWrite | QIODevice::Text);
        file.write(test_show->toPlainText().toUtf8());
        file.close();
        test_show->clear();
        qDebug()<<"write ok";
    }
}
